package com.mercenary.wein.screenControl.ui.batch;

import com.mercenary.wein.screenControl.bean.CommandDetailBean;
import com.mercenary.wein.screenControl.bean.GroupMetaBean;
import com.mercenary.wein.screenControl.bean.ScheduleTask;
import com.mercenary.wein.screenControl.service.CommandService;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class CurrentTask extends JDialog {
    JScrollPane J1 = new JScrollPane();
    JPanel J2 = new JPanel();//面板2
//    JButton j1 = new JButton("全选");
//    JButton j2 = new JButton("反选");
//    JButton j3 = new JButton("重置");
    JButton j4 = new JButton("停止任务");
//    JButton j5 = new JButton("删除选中任务");
    static JFrame locationjf = new JFrame();
    static CommandService commandService = new CommandService();
    final List<CommandDetailBean> commands;
    public CurrentTask() {
        super(locationjf, "任务列表");
        setBounds(200, 100, 700, 550);
        setLayout(new BorderLayout());
        setVisible(true);
        add(J1, BorderLayout.NORTH);
        add(J2, BorderLayout.CENTER);
        J1.setPreferredSize(new Dimension(700, 450));
        J1.setBackground(Color.WHITE);
        J2.setPreferredSize(new Dimension(700, 90));
        J2.setBackground(Color.WHITE);
//        J2.add(j1);
//        J2.add(j2);
//        J2.add(j3);
        J2.add(j4);
//        J2.add(j5);
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("序号");
        columnNames.add("手机号码");
        columnNames.add("创建时间");
        columnNames.add("事件类型");
        columnNames.add("任务内容");
        columnNames.add("执行状态");

        Vector<List<String>> columnData = new Vector<List<String>>();
        DefaultTableModel dm = new DefaultTableModel(columnData, columnNames);
        final MyJtable commandTable = new MyJtable(dm);
        commands = commandService.getCommandList();
        if(commands!=null){
            for(CommandDetailBean command : commands){
                dm.addRow(new String[]{command.getId()+"",command.getdName(),command.getSubmit_time(),
                        command.getActionName(),command.getContent(),command.getStatus()});
            }
        }
        dm.fireTableDataChanged();
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
        J1.setViewportView(commandTable);
        tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作用一样
        commandTable.setDefaultRenderer(Object.class, tcr);
        commandTable.setPreferredScrollableViewportSize(new Dimension(100, 20));
        J1.validate();

        J2.validate();

        final JPopupMenu popupMenu = new JPopupMenu();
        j4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int i = popupMenu.getComponentCount();//先要移除原有的项目
                while(i>0){
                    popupMenu.remove(0);
                    i--;
                }
                List<ScheduleTask> tasks = commandService.getSchdualJobs();
                for(final ScheduleTask task:tasks){
                    JMenuItem item = new JMenuItem(task.getJobName());
                    item.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            int result = JOptionPane.showConfirmDialog(null,"任务名称："+task.getJobName(), "是否删除任务",JOptionPane.OK_CANCEL_OPTION);
                            if (result == 0) {
                                commandService.removeJob(task);
                            }
                        }
                    });
                    popupMenu.add(item);
                }
                popupMenu.show(j4,e.getX(),e.getY());
            }
        });
    }

    public static void main(String[] arge) {
        new CurrentTask();
    }
}

class MyJtable extends JTable {
    public MyJtable(DefaultTableModel tm) {
        super(tm);
    }


    public boolean isCellEditable(int row, int column) {
        return false;
    }
}